<template>
  <div class="overview-container">
    <!-- 顶部导航栏 -->
    <van-nav-bar
      title="账户概览"
      left-arrow
      @click-left="goBack"
      :style="{ backgroundColor: '#FFD847' }"
    />

    <!-- 加载状态 -->
    <van-loading
      v-if="loading"
      type="spinner"
      color="#FFD847"
      vertical
      class="loading-center"
    >
      加载中...
    </van-loading>

    <!-- 主体内容 -->
    <div v-else class="content-wrapper">
      <!-- 总体统计卡片 -->
      <div class="stats-card total-stats">
        <div class="card-header">
          <van-icon name="bar-chart-o" />
          <span>总体统计</span>
        </div>
        <div class="stats-row">
          <div class="stat-item">
            <span class="label">总收入</span>
            <span class="value income">¥{{ overviewData.totalIncome.toFixed(2) }}</span>
          </div>
          <div class="stat-item">
            <span class="label">总支出</span>
            <span class="value expense">¥{{ overviewData.totalExpense.toFixed(2) }}</span>
          </div>
          <div class="stat-item">
            <span class="label">总结余</span>
            <span class="value balance" :class="overviewData.totalBalance >= 0 ? 'positive' : 'negative'">
              ¥{{ overviewData.totalBalance.toFixed(2) }}
            </span>
          </div>
        </div>
      </div>

      <!-- 本月统计卡片 -->
      <div class="stats-card monthly-stats">
        <div class="card-header">
          <van-icon name="calendar-o" />
          <span>本月统计</span>
        </div>
        <div class="stats-grid">
          <div class="grid-item">
            <div class="grid-label">本月收入</div>
            <div class="grid-value income">¥{{ overviewData.monthlyIncome.toFixed(2) }}</div>
          </div>
          <div class="grid-item">
            <div class="grid-label">本月支出</div>
            <div class="grid-value expense">¥{{ overviewData.monthlyExpense.toFixed(2) }}</div>
          </div>
          <div class="grid-item">
            <div class="grid-label">本月结余</div>
            <div class="grid-value balance" :class="overviewData.monthlyBalance >= 0 ? 'positive' : 'negative'">
              ¥{{ overviewData.monthlyBalance.toFixed(2) }}
            </div>
          </div>
        </div>
      </div>

      <!-- 本年统计卡片 -->
      <div class="stats-card yearly-stats">
        <div class="card-header">
          <van-icon name="clock-o" />
          <span>本年统计</span>
        </div>
        <div class="stats-grid">
          <div class="grid-item">
            <div class="grid-label">本年收入</div>
            <div class="grid-value income">¥{{ overviewData.yearlyIncome.toFixed(2) }}</div>
          </div>
          <div class="grid-item">
            <div class="grid-label">本年支出</div>
            <div class="grid-value expense">¥{{ overviewData.yearlyExpense.toFixed(2) }}</div>
          </div>
          <div class="grid-item">
            <div class="grid-label">本年结余</div>
            <div class="grid-value balance" :class="overviewData.yearlyBalance >= 0 ? 'positive' : 'negative'">
              ¥{{ overviewData.yearlyBalance.toFixed(2) }}
            </div>
          </div>
        </div>
      </div>

      <!-- 支出分类统计 -->
      <div class="category-card" v-if="overviewData.expenseByCategory && overviewData.expenseByCategory.length > 0">
        <div class="card-header">
          <van-icon name="chart-trending-o" />
          <span>支出分类统计</span>
        </div>
        <div class="category-list">
          <div 
            v-for="(item, index) in overviewData.expenseByCategory.slice(0, 5)" 
            :key="index"
            class="category-item"
          >
            <div class="category-info">
              <span class="category-name">{{ item.category }}</span>
              <span class="category-percent">{{ item.percentage.toFixed(1) }}%</span>
            </div>
            <div class="category-bar-wrapper">
              <div class="category-bar" :style="{ width: item.percentage + '%' }"></div>
            </div>
            <div class="category-amount">¥{{ item.amount.toFixed(2) }}</div>
          </div>
        </div>
      </div>

      <!-- 收入分类统计 -->
      <div class="category-card" v-if="overviewData.incomeByCategory && overviewData.incomeByCategory.length > 0">
        <div class="card-header">
          <van-icon name="gold-coin-o" />
          <span>收入分类统计</span>
        </div>
        <div class="category-list">
          <div 
            v-for="(item, index) in overviewData.incomeByCategory.slice(0, 5)" 
            :key="index"
            class="category-item"
          >
            <div class="category-info">
              <span class="category-name">{{ item.category }}</span>
              <span class="category-percent">{{ item.percentage.toFixed(1) }}%</span>
            </div>
            <div class="category-bar-wrapper income-bar">
              <div class="category-bar" :style="{ width: item.percentage + '%' }"></div>
            </div>
            <div class="category-amount">¥{{ item.amount.toFixed(2) }}</div>
          </div>
        </div>
      </div>

      <!-- 最近交易记录 -->
      <div class="recent-card" v-if="overviewData.recentRecords && overviewData.recentRecords.length > 0">
        <div class="card-header">
          <van-icon name="notes-o" />
          <span>最近交易记录</span>
        </div>
        <div class="recent-list">
          <div 
            v-for="record in overviewData.recentRecords" 
            :key="record.id"
            class="recent-item"
          >
            <div class="recent-icon" :class="record.type === 1 ? 'expense' : 'income'">
              <van-icon :name="record.type === 1 ? 'minus' : 'plus'" />
            </div>
            <div class="recent-info">
              <div class="recent-category">{{ record.category }}</div>
              <div class="recent-date">{{ record.date }}</div>
              <div class="recent-remark" v-if="record.remark">{{ record.remark }}</div>
            </div>
            <div class="recent-amount" :class="record.type === 1 ? 'expense' : 'income'">
              {{ record.type === 1 ? '-' : '+' }}¥{{ record.amount.toFixed(2) }}
            </div>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <van-empty
        v-if="!overviewData.totalRecords || overviewData.totalRecords === 0"
        description="暂无记账记录"
        image="search"
      />

      <!-- 总记录数 -->
      <div class="total-records" v-if="overviewData.totalRecords > 0">
        <van-icon name="records" />
        <span>共 {{ overviewData.totalRecords }} 笔记账记录</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getAccountOverview } from '@/api'
import { showToast } from 'vant'
import { useRouter } from 'vue-router'

const router = useRouter()
const loading = ref(true)
const overviewData = ref({
  totalIncome: 0,
  totalExpense: 0,
  totalBalance: 0,
  monthlyIncome: 0,
  monthlyExpense: 0,
  monthlyBalance: 0,
  yearlyIncome: 0,
  yearlyExpense: 0,
  yearlyBalance: 0,
  expenseByCategory: [],
  incomeByCategory: [],
  recentRecords: [],
  totalRecords: 0
})

// 返回上一页
const goBack = () => {
  router.back()
}

// 获取账户概览数据
const fetchOverviewData = async () => {
  loading.value = true
  try {
    const data = await getAccountOverview()
    console.log('账户概览数据:', data)
    overviewData.value = data
  } catch (error) {
    console.error('获取账户概览数据失败:', error)
    showToast('获取数据失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchOverviewData()
})
</script>

<style lang='scss' scoped>
.overview-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 20px;

  // 返回箭头样式
  :deep(.van-nav-bar__arrow) {
    color: #333;
    font-size: 18px;
    font-weight: 600;
  }

  .loading-center {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .content-wrapper {
    padding: 10px;
  }

  // 卡片通用样式
  .stats-card,
  .category-card,
  .recent-card {
    background-color: #fff;
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

    .card-header {
      display: flex;
      align-items: center;
      margin-bottom: 16px;
      font-size: 16px;
      font-weight: 600;
      color: #333;

      .van-icon {
        font-size: 20px;
        color: #FFD847;
        margin-right: 8px;
      }
    }
  }

  // 总体统计卡片
  .total-stats {
    background: linear-gradient(135deg, #FFD847 0%, #FFC107 100%);
    color: #333;

    .stats-row {
      display: flex;
      justify-content: space-around;
      text-align: center;

      .stat-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        gap: 8px;

        .label {
          font-size: 14px;
          color: rgba(51, 51, 51, 0.8);
        }

        .value {
          font-size: 20px;
          font-weight: bold;
          color: #333;
        }
      }
    }
  }

  // 本月/本年统计网格
  .stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;

    .grid-item {
      text-align: center;
      padding: 12px 8px;
      background-color: #f9f9f9;
      border-radius: 8px;

      .grid-label {
        font-size: 12px;
        color: #666;
        margin-bottom: 8px;
      }

      .grid-value {
        font-size: 16px;
        font-weight: 600;

        &.income {
          color: #52c41a;
        }

        &.expense {
          color: #ff6b6b;
        }

        &.balance.positive {
          color: #1890ff;
        }

        &.balance.negative {
          color: #ff4d4f;
        }
      }
    }
  }

  // 分类统计列表
  .category-list {
    display: flex;
    flex-direction: column;
    gap: 16px;

    .category-item {
      .category-info {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;

        .category-name {
          font-size: 14px;
          color: #333;
          font-weight: 500;
        }

        .category-percent {
          font-size: 13px;
          color: #666;
        }
      }

      .category-bar-wrapper {
        width: 100%;
        height: 8px;
        background-color: #f0f0f0;
        border-radius: 4px;
        overflow: hidden;
        margin-bottom: 6px;

        .category-bar {
          height: 100%;
          background: linear-gradient(90deg, #ff9800 0%, #ff6b6b 100%);
          border-radius: 4px;
          transition: width 0.5s ease;
        }

        &.income-bar .category-bar {
          background: linear-gradient(90deg, #52c41a 0%, #73d13d 100%);
        }
      }

      .category-amount {
        font-size: 14px;
        font-weight: 600;
        color: #333;
        text-align: right;
      }
    }
  }

  // 最近交易记录
  .recent-list {
    display: flex;
    flex-direction: column;
    gap: 12px;

    .recent-item {
      display: flex;
      align-items: center;
      padding: 12px;
      background-color: #f9f9f9;
      border-radius: 8px;
      transition: background-color 0.2s;

      &:active {
        background-color: #f0f0f0;
      }

      .recent-icon {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 12px;
        font-size: 18px;
        flex-shrink: 0;

        &.expense {
          background-color: #ffe5e5;
          color: #ff6b6b;
        }

        &.income {
          background-color: #e5f7e5;
          color: #52c41a;
        }
      }

      .recent-info {
        flex: 1;
        min-width: 0;

        .recent-category {
          font-size: 15px;
          color: #333;
          font-weight: 500;
          margin-bottom: 4px;
        }

        .recent-date {
          font-size: 12px;
          color: #999;
          margin-bottom: 2px;
        }

        .recent-remark {
          font-size: 12px;
          color: #666;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }

      .recent-amount {
        font-size: 16px;
        font-weight: 600;
        flex-shrink: 0;
        margin-left: 12px;

        &.expense {
          color: #ff6b6b;
        }

        &.income {
          color: #52c41a;
        }
      }
    }
  }

  // 总记录数
  .total-records {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    color: #999;
    font-size: 14px;

    .van-icon {
      margin-right: 6px;
      font-size: 16px;
    }
  }

  // 空状态
  :deep(.van-empty) {
    margin-top: 60px;
  }
}
</style>
